/**********************************************************************************
 ***
 ***    Screencasting Teaching System C++ Library
 ***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
 ***                        Wadi Jalil Maluf <wadijm@gmail.com>
 ***
 ***    This file is part of the Screencasting Teaching System C++ Library.
 ***
 ***    This library is free software; you can redistribute it and/or modify
 ***    it under the terms of the GNU General Public License as published by
 ***   the Free Software Foundation; either version 3 of the License, or
 ***    (at your option) any later version.
 ***
 ***    This library is distributed in the hope that it will be useful,
 ***    but WITHOUT ANY WARRANTY; without even the implied warranty of
 ***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ***   GNU General Public License for more details.
 ***
 ***    You should have received a copy of the GNU General Public License
 ***    along with this library; if not, write to the Free Software
 ***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ***
 *********************************************************************************/

#include <iostream>
#include <string>
#include <cassert>
#include "clientservice.h"
#include "scpersistenceservice.h"
#include "../common/comm/operation.h"
/**
 *
 */
ClientService::ClientService() :
logger_(log4cxx::Logger::getLogger("server.client_service")) {
}

/**
 *
 */
ClientService::~ClientService() {
  for (std::list<User*>::iterator it = users.begin(); it != users.end(); it++)
    delete *it;
  for (std::list<Course*>::iterator it = courses.begin(); it != courses.end(); it++)
    delete *it;
  for (std::list<Lesson*>::iterator it = lessons.begin(); it != lessons.end(); it++)
    delete *it;
}

/**
 *
 */
void ClientService::loadData(std::istream &xmlCoursesAndLessons, std::istream &xmlusers) {
  LOG4CXX_DEBUG(logger_, "loading users");
  this->xmlUtil.loadUsers(xmlusers, users);
  LOG4CXX_DEBUG(logger_, "loading courses and lessons");
  this->xmlUtil.loadShows(xmlCoursesAndLessons, courses, lessons);
}

Lesson*  ClientService::startLesson(Lesson &lessonName){
    Lesson* l=findLesson(lessonName);
    for (std::list<User*>::iterator it = l->getStudents().begin(); it != l->getStudents().end(); it++) {
         User* user= *it;
         assert(user!=0 && " User is null, could not find lesson");
         if (isUserLogged(user->getId())){
             user->setListeningLesson(true);
         }

     }
    return l;
}

bool ClientService::isUserLogged(int idUser) {
  for (std::list<User*>::iterator it = users.begin(); it != users.end(); it++) {
      User* user = *it;
      if (user->getId()==idUser){
        return user->isLogged();
      }
  }
  return false;
}
/**
 *
 */
Lesson* ClientService::findLesson(Lesson &lessonRequested) {
  Lock l(mutex);
  for (std::list<Lesson*>::iterator it = lessons.begin(); it != lessons.end(); it++) {
      Lesson* lesson = *it;
      assert(lesson!=0 && " Lesson is null, could not find lesson");
      LOG4CXX_TRACE(logger_, "Comparing " << lessonRequested.getId() << " with " << lesson->getId());
      if (lessonRequested.getId()==lesson->getId())
        return lesson;
  }

  for (std::list<Course*>::iterator it = courses.begin(); it != courses.end(); it++) {
      Course* course = *it;
      assert(course!=0 && " Course is null, could not find course");
      for (std::list<Lesson*>::iterator it = course->getLessons().begin(); it != course->getLessons().end(); it++) {
          Lesson* lesson = *it;
          assert(lesson!=0 && " Lesson is null, could not find lesson");
          LOG4CXX_TRACE(logger_, "Comparing " << lessonRequested.getId() << " with " << lesson->getId());
          if (lessonRequested.getId()==lesson->getId())
            return lesson;
      }
  }
  LOG4CXX_WARN(logger_, "Returning null - Lesson not found!");
  return NULL;
}

/**
 *
 */
User* ClientService::login(std::string username, std::string password) {
  Lock l(mutex);
  for (std::list<User*>::iterator it = users.begin(); it != users.end(); it++) {
      User* user = *it;
      if (username.compare(user->getUser()) == 0 && password.compare(user->getPassword()) == 0){
        return user;
      }
  }
  return NULL;
}
std::list<Course*> * ClientService::findCourses(User* user) {
  Lock l(mutex);
  std::list<Course*> *userCourses=new std::list<Course*>();
  for (std::list<Course*>::const_iterator i = courses.begin(); i != courses.end(); ++i) {
      // is public or is subscribed
      if ((*i)->getCode().empty() || (*i)->isSubscribed(user)) {
          userCourses->push_back(*i);
      }
  }
  return userCourses;
}


std::list<Lesson*> * ClientService::findLessons(User* user) {
  Lock l(mutex);
  std::list<Lesson*> *userCourses=new std::list<Lesson*>();
  for (std::list<Lesson*>::const_iterator i = lessons.begin(); i != lessons.end(); ++i) {
        // is public or is subscribed
        if ((*i)->getCode().empty() || (*i)->isSubscribed(user) ){
            userCourses->push_back(*i);
        }
    }
  return userCourses;
}

